package org.codersunit.radboid.test;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;

public class RadboidTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {    	
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int rounds = 20;
        long[] times = new long[rounds];
        for(int i = 0; i < rounds; i++) {
        long startTime = System.currentTimeMillis();
        URI url = null;
		try {
			url = new URI("http://rooster.ru.nl/Default.aspx");
		} catch (URISyntaxException use) {
			Log.e("RadboidTest", "Invalid url", use);
		}
		
		if(url != null) {
	        HttpClient client = new DefaultHttpClient();
	        HttpGet getRequest = new HttpGet(url);
	        HttpResponse response;
	        
	        try {
	        	response = client.execute(getRequest);
	        	Log.i("RadboidTest", response.getStatusLine().toString());
	        	HttpEntity entity = response.getEntity();
	        	InputStreamReader reader = new InputStreamReader(entity.getContent());
	        	
	        	ListElementsParser parser = new ListElementsParser();
	        	parser.parse(reader);
	        	HttpPost postRequest = new HttpPost(url);
	        	List<NameValuePair> params = new ArrayList<NameValuePair>();
	        	params.add(new BasicNameValuePair("__VIEWSTATE", parser.getViewState()));
	        	params.add(new BasicNameValuePair("__EVENTVALIDATION", parser.getEventValidation()));
	        	params.add(new BasicNameValuePair("__EVENTTARGET", "LinkBtn_modules"));
	        	params.add(new BasicNameValuePair("tLinkType", "modules"));
	        	postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
	        	response = client.execute(postRequest);
	        	entity = response.getEntity();
	        	reader = new InputStreamReader(entity.getContent());
	        	
	        	
	        	ScheduleIndexParser schedule = new ScheduleIndexParser();
	        	schedule.parse(reader);
	        	
	        } catch(ClientProtocolException cpe) {
	        	Log.e("RadboidTest", "Http error", cpe);
	        } catch(IOException ioe) {
	        	Log.e("RadboidTest", "Failed reading source", ioe);
	        }
		}
		times[i] = System.currentTimeMillis() - startTime;
		Log.i("RadboidSpeedTest", String.format("RadboidSpeedTest: Round %d/%d finished after %d millis", i + 1, rounds, times[i]));
        }
        long total = 0L;
        for(int i = 0; i < rounds; i++) {
        	total += times[i];
        }
        Log.i("RadboidSpeedTest", String.format("RadboidSpeedTest: Average time required in %d rounds: %d millis", rounds, total / rounds));
    }
    
	public class CodePointBuffer {
		private char[] storage;
		private int current;
		
		public CodePointBuffer(int capacity) {
			storage = new char[capacity];
			current = 0;
		}
		
		public void expand(int minNewSize) {
			int newsize = storage.length;
			while(newsize <= minNewSize) {
				newsize = newsize * 2;
			}
			char[] newStorage = new char[newsize];
			System.arraycopy(storage, 0, newStorage, 0, current);
			storage = newStorage;
		}
		
		public void clear() {
			current = 0;
		}
		
		public void appendCodePoint(int code) {
			if(current >= storage.length) {
				expand(current);
			}
			storage[current] = (char) code;
			current += 1;
		}
		
		@Override
		public String toString() {
			return new String(storage, 0, current);
		}
		
		public int length() {
			return current;
		}
	}
    
}